package name.jiaosha.air.company.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.Department;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

/**
 * 部门控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/com")
public class DepartmentController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/departmentList")
	public ModelAndView getDepartmentList() {

		PageInfo<Department> pageInfo = null;
		if (!AppConstants.DEBUG) {
			List<Department> list = new ArrayList<Department>();
			for (int i = 1; i <= 20; i++) {

				Department dep = new Department();
				dep.setId(i);
				dep.setName("技术部");
				dep.setPinyin(PinyinToolkit.cn2FirstSpell(dep.getName()));
				list.add(dep);
			}

		} else {

			pageInfo = companyService.getDepartmentByComId(AppConstants.comId,
					1, AppConstants.DEFAULT_SIZE);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createDepartment")
	public ModelAndView create(@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {
			Department department = new Department();
			department.setComId(AppConstants.comId);
			department.setName(name.trim());
			companyService.createDepartment(department);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateDepartment")
	public ModelAndView update(@RequestParam int id, @RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Department department = companyService.getDepartmentById(id);
			if (department != null) {
				department.setName(name.trim());
				companyService.updateDepartment(department);

			}
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	public List<Department> getMyDepartmentList() {

		// List<Department> list = companyService.getDepartmentByComId(null);

		return null;
	}
}
